<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<script>
if (parent.addform.photo.value == '')
{
	var img_str = '<img src="uploadfiles/image/nopic.jpg" width="90" border="0" alt="">'; 
}
else
{
    //document.img_upload.file.value = parent.addform.photo.value;
	var img_str = '<img src="'+parent.addform.photo.value+'" width="90" border="0" alt="">';
}
</script>
<style type="text/css">
<!--
body {
	margin-left: 0px;
	margin-top: 0px;
	margin-right: 0px;
	margin-bottom: 0px;
}
-->
</style>
</head>
<body>
<?php
/* css	*/	
//include_once('../include/comm_include_for_css.php');
include_once('../include/functions.php');
$action=fs_get_value("do");
switch($action)
{
	case	"upload_img"	     : upload_img()	         ; break;
	default				         : main()		         ; break;
}

?>
<?php
function main(){
?>
<script>document.write(img_str);</script>
<form name="img_upload" method="post" action="?do=upload_img" enctype="multipart/form-data">
<input type="file" style="width:245px" name="file" value="" />
<input type="submit" value="上传" />
</form>
<?php
}	
?>
<?php
function upload_img(){
   $size = 10240000;//设置上传图片的最大值，B字节
   $uploaddir = "uploadfiles/image/";//设置文件保存目录 注意包含/    
   //$patch="http://localhost/uploadfiles/image/";//程序所在路径
   $type = array("jpg","gif","bmp","jpeg","png");//设置允许上传文件的类型 
   $a=strtolower(fileext($_FILES['file']['name']));
   //判断文件类型
   if(!in_array(strtolower(fileext($_FILES['file']['name'])),$type))
   {
        $text=implode(",",$type);
        exit("<span style='width:280px; height:50px;line-height:25px;font-size:13px;background-color:#EDEDED;color:#FF0000;'>上传失败！您只能上传以下类型文件:<br /> ".$text."&nbsp;&nbsp;&nbsp;&nbsp;<a href='?'>重新上传!</a></span>");
   }
   //生成目标文件的文件名    
   else{
	   if($_FILES["file"]["size"] > $size){
?>
<script>document.write(img_str);</script>
<div style="width:280px; height:20px; line-height:20px; background-color:#EDEDED;font-size:13px;margin:5px 0px;">上传失败，图片大小超出了限制！<a href="?">重新上传!</a></div>
<?php
	       exit;
	   }
       $filename=explode(".",$_FILES['file']['name']);
       do
       {
           $filename[0]=date("ymdHis").rand(1000,9999); //设置随机数
           $name=implode(".",$filename);
           //$name1=$name.".Mcncc";
           $uploadfile=$uploaddir.$name;
       }
		while(file_exists($uploadfile));
		if(is_uploaded_file($_FILES['file']['tmp_name'])){ //判断是否合法上传
		    if (move_uploaded_file($_FILES['file']['tmp_name'],$uploadfile)){
?>
<script type="text/javascript">
parent.addform.photo.value='<?php echo $uploadfile; ?>';
</script>
<img src="<?php echo($uploadfile);?>" width="90" border="0" alt=""><span style="font-size:13px;">--预览</span>
<div style="width:280px; height:20px; line-height:20px; background-color:#EDEDED;font-size:13px;margin:5px 0px;">上传成功！</div>
<?php
			}
			else{
?>
<script>document.write(img_str);</script>
<div style="width:280px; height:20px; line-height:20px; background-color:#EDEDED;font-size:13px;margin:5px 0px;">上传失败！<a href="?">重新上传!</a></div>
<?php
			}
		}
   }
?>
<?php
}
function fileext($filename)
{
    return substr(strrchr($filename, '.'), 1);
}
?>
</body>
</html>
